package zhh.tangbao.auxiliary.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhh.tangbao.auxiliary.dao.DataStatisticsLogCommodityDao;
import zhh.tangbao.auxiliary.dao.DataStatisticsLogDao;
import zhh.tangbao.auxiliary.dto.statistics.LogQueryDto;
import zhh.tangbao.auxiliary.entity.DataStatisticsLog;
import zhh.tangbao.auxiliary.entity.DataStatisticsLogCommodity;
import zhh.tangbao.auxiliary.service.DataStatisticsService;
import zhh.tangbao.auxiliary.vo.statistics.DataStatisticsInfoVo;
import zhh.tangbao.commodity.dao.CommodityEntityDao;
import zhh.tangbao.commodity.vo.commodity.CustomCommodityVo;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 描述
 * </p>
 *
 * @author 粥灰灰
 * <p>2023/4/27</p>
 */

@Service
public class DataStatisticsServiceImpl implements DataStatisticsService {

    @Autowired
    private DataStatisticsLogDao logDao;

    @Autowired
    private DataStatisticsLogCommodityDao commodityLogDao;

    @Autowired
    private CommodityEntityDao commodityEntityDao;

    @Override
    public Page<DataStatisticsLog> logPage(LogQueryDto dto) {
        Page<DataStatisticsLog> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        QueryWrapper<DataStatisticsLog> query = new QueryWrapper<>();
        query.orderByDesc("record_time");
        if (null != dto.getStart() && null != dto.getEnd()) {
            query.between("record_time", dto.getStart(), dto.getEnd());
        }
        return logDao.selectPage(page, query);
    }

    @Override
    public DataStatisticsInfoVo getInfo(Integer id) {
        DataStatisticsLog dataStatisticsLog = logDao.selectById(id);
        Assert.notNull(dataStatisticsLog, "查询不到此记录信息");
        DataStatisticsInfoVo vo = new DataStatisticsInfoVo(dataStatisticsLog);
        List<DataStatisticsLogCommodity> commodities = commodityLogDao.selectList(new QueryWrapper<DataStatisticsLogCommodity>().eq("data_statistics_log_id", dataStatisticsLog.getId()));
        if (CollUtil.isEmpty(commodities)) {
            return vo;
        }
        vo.setCommodities(commodities);
        // 不直接联表查询是为了防止当商品实体信息被删除时会导致展示的销售记录出现异常
        List<CustomCommodityVo> c = commodityEntityDao.getCommodityNameByIds(commodities.stream().map(DataStatisticsLogCommodity::getCommodityEntityId).collect(Collectors.toList()));
        if (CollUtil.isEmpty(c)) {
            return vo;
        }
        Map<String, CustomCommodityVo> map = c.stream().collect(Collectors.toMap(CustomCommodityVo::getId, Function.identity()));
        for (int i = 0; i < commodities.size(); i++) {
            DataStatisticsLogCommodity commodity = commodities.get(i);
            CustomCommodityVo ccvo = map.get(commodity.getCommodityEntityId());
            if (null != ccvo) {
                commodity.setCommodityEntityName(ccvo.getCommodityName());
            }
        }
        return vo;
    }
}
